package com.hudongwx.godoffood.fragment.communityFragment.viewPager;

import android.graphics.Color;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hudongwx.godoffood.R;

import org.xutils.common.util.DensityUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 滑动的页面
 */
@ContentView(R.layout.activity_image)
public class ImageActivity extends AppCompatActivity {
    @ViewInject(R.id.image_viewpager)
    private ViewPager viewPager;
    private String TAG="ImageActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        init();
    }

    private void init() {
        ArrayList<String> infoList = getIntent().getStringArrayListExtra("list");
        int position = getIntent().getIntExtra("position", 0);
        MyAdapter myadapter = new MyAdapter(infoList);
        viewPager.setAdapter(myadapter);
        viewPager.setCurrentItem(position);

    }
    //viewpager的适配器
    class MyAdapter extends PagerAdapter {
        ArrayList<String> data;
        public MyAdapter(ArrayList<String> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //创建图片
            RelativeLayout relativeLayout = new RelativeLayout(ImageActivity.this);
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            relativeLayout.setGravity(Gravity.CENTER);
            relativeLayout.setLayoutParams(layoutParams2);
            relativeLayout.setBackgroundColor(Color.BLACK);
            ImageView imageView = new ImageView(ImageActivity.this);
            LinearLayoutCompat.LayoutParams layoutParams = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(400));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(layoutParams);
            String imageUrl = data.get(position);
            x.image().bind(imageView, imageUrl);
            relativeLayout.addView(imageView);
            relativeLayout.setOnClickListener(new imageClickListener());
            container.addView(relativeLayout);
            return relativeLayout;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //移除
            container.removeView((View) object);


        }
    }

    class imageClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            finish();
        }
    }

}